#include "productdelegate.h"
#include <QPainter>
#include "dataview/productmodel.h"

ProductDelegate::ProductDelegate(QObject* parent):QAbstractItemDelegate(parent)
{

}

void ProductDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    painter->save();
    QColor color(30, 60, 150);
    QString showName = index.data(ProductModel::ROLE_NAME).toString();
    QString hi = index.data(ProductModel::ROLE_HI).toString();
    QString ok = index.data(ProductModel::ROLE_OK).toString();
    QString lo = index.data(ProductModel::ROLE_LOW).toString();
    hi.prepend("H:");ok.prepend("S:");lo.prepend("L:");
    QRect nameRe,hiRe,okRe,loRe;
    nameRe = QRect(option.rect.left(),option.rect.top(),option.rect.width(),option.rect.height()-60);
    hiRe = QRect(option.rect.left(),option.rect.top()+nameRe.height(),option.rect.width(),20);
    okRe = QRect(option.rect.left(),option.rect.top()+nameRe.height()+20,option.rect.width(),20);
    loRe = QRect(option.rect.left(),option.rect.top()+nameRe.height()+40,option.rect.width(),20);
    if(!showName.isEmpty())
    {
        if(option.state&QStyle::State_Selected)
        {
            painter->setBrush(Qt::darkBlue);
        }
        else {
//            QLinearGradient lg(0,0,0,option.rect.height());
//            lg.setColorAt(1,QColor(134,187,237,255));
//            lg.setColorAt(0,QColor(0,16,67,255));
            //QBrush brush(lg);
            painter->setBrush(color);
        }
        painter->drawRect(option.rect);
        painter->setPen(Qt::white);
        painter->setFont(QFont("Ubuntu Bold",25));
        painter->drawText(nameRe,showName,Qt::AlignVCenter|Qt::AlignHCenter);
        painter->setPen(Qt::red);
        painter->setFont(QFont("Ubuntu Regula",15));
        painter->drawText(hiRe,hi,Qt::AlignVCenter|Qt::AlignHCenter);
        painter->setPen(Qt::green);
        painter->drawText(okRe,ok,Qt::AlignVCenter|Qt::AlignHCenter);
        painter->setPen(QColor("#FF640A"));
        painter->drawText(loRe,lo,Qt::AlignVCenter|Qt::AlignHCenter);
    }
    painter->restore();
}

QSize ProductDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if(index.isValid())
        return QSize(55,105);
}

